var Md5 = require('../../utils/md5.js');
var timeSecond = Math.floor(Date.now() * 0.001);  //获取时间戳: 
var SignInfoData = Md5.hexMD5(timeSecond + 'nativetalk')//md5加密;
var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
var httpNativetalk = app.globalData.httpNativetalk;
var httpWeike = app.globalData.httpWeike;
var httpFile = 'http://file.waijiaojun.com/';
var weikeid, set_amout, staridx, intro_video;
var pageNum = 1, systemDataArr, systemDataIdArr = [], commentAreaValue, flag = true, salesPromotion_id;
Page({
  data: {
    navIdx: 0,
    navBar: ['简介', '课表'],
    navComment: ['评价'],
    subscribelFlag: true,
    pushFlag: true,
    imgUrl: 'http://file.waijiaojun.com/waijiaojun/icon',
    systemFlag: false,
    commentLayerFlag: false,
    staridx: 5,
    isVideo: true,
  },  
  onLoad: function (e) {
    weikeid = e.weikeId;
    var that = this;
    that.classDataFun(weikeid);
    that.classCommentFun(weikeid);
  },
  
  //简介与课表的请求数据的请求
  classDataFun: function (e) {
    var that = this;
    //页面数据的请求
    wx.request({
      url: httpWeike + '/v4.2/student/find/detail',
      data: {
        weike_id: e
      },
      header: {//请求头
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        "token": app.globalData.token,
      },
      method: 'POST',
      success: function (res) {
        // console.log(res);
        set_amout = res.data.obj.price;
        intro_video = res.data.obj.intro_video;
        var parts = res.data.obj.parts;
        var intro = res.data.obj.intro;
        var scoreData = res.data.obj.score;
        var priceData = res.data.obj.price;       
        res.data.obj.score = scoreData.toFixed(1);
        res.data.obj.price = priceData.toFixed(2);
        // console.log(res.data.obj.intro_video);
        if (intro_video !== null) {
          that.setData({
            isVideo: false,
          })
        };
        if (res.data.obj.memberSalesPromotion==null){
          that.setData({ pushFlag: false })
        }else{
          var text = res.data.obj.memberSalesPromotion.desc;
          salesPromotion_id = res.data.obj.memberSalesPromotion.id;
          that.setData({ text: text});
        }
       
        WxParse.wxParse('intro', 'html', intro, that, 0);
        function getDate(time) {
          var date = new Date(time);
          var Y = date.getFullYear() + '-';
          var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
          var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
          return (M + D);
        };
        for (var i = 0; i < parts.length; i++) {
          parts[i].update_date = getDate(parts[i].update_date)

        }
        that.setData({
          systemClassData: res.data.obj,
          systemClassVideoBG: 'http://file.waijiaojun.com/' + res.data.obj.cover_url,         
          weike_id: e
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })

  },
  //评论数据的请求
  classCommentFun: function (e) {
    var that = this;
    wx.request({
      url: httpWeike + '/v4.2/student/find/comments',
      data: {
        weike_id: e
      },
      header: {
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        "token": app.globalData.token,
      },
      method: 'POST',
      success: function (res) {
        // console.log(res);
        var com_timeData = res.data.obj.data;
        var com_size = [];
        com_size.push('评论(' + res.data.obj.data.length + ')');
        function getDate(time) {
          var date = new Date(time);
          var Y = date.getFullYear() + '-';
          var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
          var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
          var hour = date.getHours();
          var minute = date.getMinutes();
          return (Y + M + D);
        };
        for (var i = 0; i < com_timeData.length; i++) {
          com_timeData[i].com_time = getDate(com_timeData[i].com_time);
          if (com_timeData[i].com_reply_content == null || com_timeData[i].com_reply_content == "") {
            com_timeData[i].com_reply_content = false;
          };
        }
        that.setData({
          commentData: res.data.obj.data,
          navComment: com_size,
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //小导航
  navBarFun: function (e) {
    var navIdx = e.currentTarget.dataset.idx;
    this.setData({
      navIdx: navIdx,
    })
  },
  //跑马灯字幕关闭函数
  closeFun: function () {
    this.setData({ pushFlag: false })
    wx.request({
      url: httpWeike + '/v4.2/student/salesPromotion/close',
      data: {
        "salesPromotionId": salesPromotion_id,
      },
      header: {
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        "token": app.globalData.token,
      },
      method: 'POST',
      success: function (res) {},
      })
  },
  //订阅弹框函数
  subscribeFun: function () {
    if (app.globalData.token == null) {
      wx.navigateTo({
        url: '/pages/bindPhone/bindPhone',
      })
    }
    if (app.globalData.token !== null) {
      this.setData({
        subscribelFlag: false,
      })
    }
  },
  //顶部视频的播放函数
  intro_videoFun: function () {
    intro_video = intro_video;
    wx.navigateTo({
      url: '/pages/systemClassVideoPlay/systemClassVideoPlay?intro_video=' + intro_video,
    })
  },
  //课程详情的函数
  littleClassFun: function (e) {
    if (app.globalData.token == null) {
      wx.navigateTo({
        url: '/pages/bindPhone/bindPhone',
      })
    }
    if (app.globalData.token !== null) {    
      var ids = e.currentTarget.dataset.ids
      wx.navigateTo({
        url: '/pages/littleClass/littleClass?id=' + ids,
      })
    }
  },
  //订阅模式弹框取消按钮
  subscribeCancelFun: function (e) {
    this.setData({
      subscribelFlag: true,
    })
  },
  //订阅模式弹框确定按钮
  subscribeSureFun: function (e) {
    wx.navigateTo({
      url: '/pages/classBuy/classBuy?weikeid=' + weikeid + '&set_amout=' + set_amout,

    });
  },
  onHide: function () {
    this.setData({
      subscribelFlag: true,
    });
    systemDataIdArr = [];
  },
  onShow: function () {
    this.systemData(pageNum);
    wx.showShareMenu({
      withShareTicket: true,
    })
  },
  onUnload: function () {
    systemDataIdArr = [];
  },
  //查询判断是否买过该课程
  systemData: function (pageNum) {
    if (app.globalData.token !== null) {
      var that = this;
      wx.request({
        url: httpWeike + '/v4.2/student/token/my/weike',
        data: {
          "currentPage": pageNum,
          "pageSize": 10000,
        },
        header: {
          "Timestamp": timeSecond,
          "SignInfo": SignInfoData,
          "content-Type": "application/json",
          "client_type": "3",
          "version": 15,
          'token': app.globalData.token,
        },
        method: 'POSt',
        success: function (res) {
          // console.log(res);
          systemDataArr = res.data.obj.data;
          for (var i = 0; i < systemDataArr.length; i++) {
            systemDataIdArr.push(systemDataArr[i].id);
          }
          for (var i = 0; i < systemDataIdArr.length; i++) {
            if (weikeid == systemDataIdArr[i]) {
              that.setData({
                systemFlag: true,
              });
              return;
            }
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  //弹出评论弹框
  commentClassFun: function () {
    this.setData({
      commentLayerFlag: true,
    })
  },
  //关闭评论弹框函数
  closeCommentLayerFun: function () {
    this.setData({
      commentLayerFlag: false,
    })
  },
  //星级评价函数
  tapStarFun: function (e) {
    staridx = e.currentTarget.dataset.staridx;
    this.setData({
      staridx: staridx,
    })
  },
  //失焦事件事件
  textBlur: function (e) {
    console.log('输入的内容为' + e.detail.value);
    var that = this;
    if (e.detail && e.detail.value.length > 0) {
      if (e.detail.value.length <= 200) {
        //showToast('内容为12-500个字符','loading',1200);
      } else {
        that.setData({
          commentAreaValue: e.detail.value
        });
      }
    } else {
      that.setData({
        commentAreaValue: '',
      });
      commentAreaValue = '';
    }
  },
  //评论的提交函数
  subimtFormFun: function (e) {
    var commentValue = e.detail.value.commentTextArea;
    console.log(e);
    if (staridx == undefined) {
      staridx = 5;
    };
    console.log("星级和评论", "星级=" + staridx, "评论=" + commentValue);
    if (commentValue == undefined) {
      commentValue = '';
    }
    console.log(staridx, commentValue);
    var that = this;
    if (flag == true) {
      flag = false;

      wx.request({
        url: httpWeike + '/v4.2/student/token/save/comment',
        data: {
          "content": commentValue,
          "score": staridx,
          "weike_id": weikeid,
        },
        header: {
          "Timestamp": timeSecond,
          "SignInfo": SignInfoData,
          "content-Type": "application/json",
          "client_type": "3",
          "version": 15,
          "token": app.globalData.token,
        },
        method: 'POST',
        success: function (res) {
          flag = true;
          wx.showToast({
            title: '评论成功',
            icon: 'success',
            duration: 2000,
            success: function () {
              that.setData({
                commentLayerFlag: false,
              });
              that.classDataFun(weikeid);
              that.classCommentFun(weikeid);

            }
          });

        },
      })

    }
  },
  //防止冒泡处理
  bb: function () { },
  //未买课提示
  waringFun: function () {
    wx.showToast({
      title: '请先购买课程',
      icon: 'loading',
      duration: 2000
    })
  },
  //试听函数
  tryListrenFun: function (e) {
    if (app.globalData.token == null) {
      wx.navigateTo({
        url: '/pages/bindPhone/bindPhone',
      })
    }
    if (app.globalData.token !== null) {
      var ids = e.currentTarget.dataset.ids;
      var coverFlag = true;
      wx.navigateTo({
        url: '/pages/littleClass/littleClass?id=' + ids + '&coverFlag=' + coverFlag,
      })
    }
  }
})